﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MissionImpossible.Properties;

namespace MissionImpossible
{
  public partial class Form1 : Form
  {
    private AboutBox aboutDialog;
    private SearchDialog searchDialog;
    private PeoplesEditor peoplesEditor;
    private FolderBrowserDialog openFileDialog;
    public Form1()
    {
      InitializeComponent();
      aboutDialog = new AboutBox();
      openFileDialog = new FolderBrowserDialog();
      openFileDialog.SelectedPath = Settings.Default.HomeDirectory;
    }

    private void CreateDatabaseSpecificDialogs()
    {
      searchDialog = new SearchDialog();
      searchDialog.VisibleChanged += new EventHandler(searchDialog_VisibleChanged);
      peoplesEditor = new PeoplesEditor();
      peoplesEditor.VisibleChanged += new EventHandler(peoplesEditor_VisibleChanged);
    }

    void peoplesEditor_VisibleChanged(object sender, EventArgs e)
    {
      openEditorToolStripMenuItem.Checked = peoplesEditor.Visible;
    }

    private void dToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      aboutDialog.Visible = true;
    }

    private void findToolStripMenuItem_Click(object sender, EventArgs e)
    {
      OpenOrBringToFront(searchDialog);
    }

    private static void OpenOrBringToFront(Form form)
    {
      if (form.Visible)
      {
        form.BringToFront();
      }
      else
      {
        form.Visible = true;
      }
    }

    void searchDialog_VisibleChanged(object sender, EventArgs e)
    {
      findToolStripMenuItem.Checked = searchDialog.Visible;
    }

    private void peoplesToolStripMenuItem_Click(object sender, EventArgs e)
    {
      OpenOrBringToFront(peoplesEditor);
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
      UpdateHomeDataDirectory();
    }

    private void UpdateHomeDataDirectory()
    {
      DialogResult openFileResult = openFileDialog.ShowDialog();
      if(openFileResult == DialogResult.OK)
      {
        UpdateDataDirectory();
        CreateDatabaseSpecificDialogs();
        SetDatabaseSpecificMenuesEnabled();
      }
    }

    private void SetDatabaseSpecificMenuesEnabled()
    {
      editToolStripMenuItem.Enabled = true;
      editToolStripMenuItem1.Enabled = true;
      addToolStripMenuItem.Enabled = true;
      deleteToolStripMenuItem.Enabled = true;
      findToolStripMenuItem.Enabled = true;
      peoplesToolStripMenuItem.Enabled = true;
      openEditorToolStripMenuItem.Enabled = true;
    }

    private void UpdateDataDirectory()
    {
      AppDomain.CurrentDomain.SetData("DataDirectory", openFileDialog.SelectedPath);
    }
  }
}
